package com.java.base;
import java.util.Scanner;
public class MyFiveChess {
    static char[][] chess=new char[16][16];
    static boolean isBlack=true;

	public static void main(String[] args) {
		for(int i=0;i<chess.length;i++){
			for(int j=0;j<chess[i].length;j++){
				chess[i][j]='*';
			}
		}
		printChess();
		Scanner sca=new Scanner(System.in);
		while(true){
            System.out.println("请"+(isBlack?"黑":"白")+"方下棋子：");
			String str=sca.next();
			int i=charToInt(str.charAt(0));
			int j=charToInt(str.charAt(1));
			if(chess[i][j]!='*'){
				System.out.println("该位置已经有棋子了，请重新输入：");
				continue;
			}else{
				chess[i][j]=isBlack?'@':'O';
				printChess();
				if(isWin(i,j)){
					System.out.println((isBlack?"黑":"白")+"方获胜！！");
					break;
				}
			}
			isBlack=!isBlack;
		}

	}
	public static void printChess(){
		for(int i=0;i<chess.length;i++){
			if(i<10){
				System.out.print("\t"+i);
			}else{
				System.out.print("\t"+(char)(i+'a'-10));
			}
		}
		System.out.println();
		for(int i=0;i<chess.length;i++){
			if(i<10){
				System.out.print(i);
			}else{
				System.out.print((char)(i+'a'-10));
			}

			for(int j=0;j<chess[0].length;j++){
				System.out.print("\t"+chess[i][j]);
			}
			System.out.println();
		}
		System.out.println();
	}
	public static int charToInt(char c){
		if(c>='0'&&c<='9'){
			return (c-'0');
		}
		return (c-'a'+10);	 
	}
	public static boolean isWin(int i,int j){
		return isWinAtS(i,j)||isWinAtH(i,j)||isWinAtZX(i,j)||isWinAtYX(i,j);
		
	}
    public static boolean isWinAtS(int i,int j){
    	char ch=chess[i][j];
    	int k=j;
    	while(k>=0&&chess[i][k]==ch){
    		k--;    		
    	}
    	k++;
    	int count=0;
    	while(k<chess.length&&chess[i][k]==ch){
    		k++;
    		count++;
    	}
    	return count>=5;
    }
     public static boolean isWinAtH(int i,int j){
    	 char ch=chess[i][j];
     	int k=i;
     	while(k>=0&&chess[k][j]==ch){
     		k--;    		
     	}
     	k++;
     	int count=0;
     	while(k<chess.length&&chess[k][j]==ch){
     		k++;
     		count++;
     	}
     	return count>=5;
     }
     public static boolean isWinAtZX(int i,int j){
    	 char ch=chess[i][j];
     	int x=i;
     	int y=j;
     	while(x>=0&&y>=0&&chess[x][y]==ch){
     		x--;
     		y--;
     	}
     	x++;
     	y++;
     	int count=0;
     	while(x<chess.length&&y<chess[0].length&&chess[x][y]==ch){
     		x++;
     		y++;
     		count++;
     	}
     	return count>=5;

     }
     public static boolean isWinAtYX(int i,int j){
    	 char ch=chess[i][j];
     	int x=i;
     	int y=j;
     	while(x>=0&&y<chess[0].length&&chess[x][y]==ch){
     		x--;
     		y++;
     	}
        x++;
     	y--;
     	int count=0;
     	while(x<chess.length&&y>=0&&chess[x][y]==ch){
     		x++;
     		y--;
     		count++;
     	}
     	return count>=5;
    	
     }
}
